package com.web.activiti.service.Impl;

import com.web.activiti.mapper.EmployeeMapper;
import com.web.activiti.mapper.SysUserRoleMapper;
import com.web.activiti.pojo.Employee;
import com.web.activiti.pojo.EmployeeExample;
import com.web.activiti.pojo.SysUserRole;
import com.web.activiti.pojo.SysUserRoleExample;
import com.web.activiti.service.EmployeeService;
import com.web.activiti.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("EmployeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public Employee findUserByUsername (String username) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria=example.createCriteria();
        criteria.andNameEqualTo(username);

        List<Employee> list = employeeMapper.selectByExample(example);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Employee findManager (long managerid) {
        return employeeMapper.selectByPrimaryKey(managerid);
    }

    @Override
    public void updateEmployeeRole (String roleId , String userId) {
        //角色和用户的连表
        SysUserRoleExample example=new SysUserRoleExample();
        SysUserRoleExample.Criteria criteria=example.createCriteria();
        criteria.andSysUserIdEqualTo(userId);
        SysUserRole userRole = sysUserRoleMapper.selectByExample(example).get(0);
        userRole.setSysRoleId(roleId);
        sysUserRoleMapper.updateByPrimaryKey(userRole);
        //员工表
        try {
            EmployeeExample employeeExample = new EmployeeExample();
            EmployeeExample.Criteria c = employeeExample.createCriteria();
            c.andNameEqualTo(userId);
            Employee employee = employeeMapper.selectByExample(employeeExample).get(0);
            System.out.println(employee.getName());
            employee.setRole(roleId);
            System.out.println(roleId+"\n111,"+employee.getRole());
            employeeMapper.updateByPrimaryKey(employee);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String addUser (Employee employee) {
        String msg="添加完成";
        employeeMapper.insert(employee);
        return msg;
    }
}
